//index.js
//获取应用实例
const app = getApp()
var url = app.globalData.domain;
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    number:3,
    items:[0,1,2,3,4,5,6,7,8],
    background: ["#F7F7F7","#F7F7F7","#F7F7F7","#F7F7F7","#F7F7F7","#F7F7F7","#F7F7F7","#F7F7F7","#F7F7F7"],
    boxColor: ["#90B5CF","#FFDB90","#B96E96","#B66600","#A0A0A0","#00000"],
    boxColorIndex: 5,
    boxUrl: "/resource/audio/click.mp3",
    colorUrl: "/resource/audio/clickNumber.mp3",
    count:0,
    isMusic: true,
    musicAngle: 0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.loadTable(3);
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /**
   * 点击盒子 AppSecret 小程序密钥 bfb5e167be90d382c2df5739b2dc1ef5  AppID(小程序ID)	wx21fb338a4b362d08
   * @param {*} e 
   */
  clickBox:function(e){
    var that = this;
    app.addClickAudio(that.data.boxUrl);
    var arr = new Array();
    
    let index = e.currentTarget.dataset.index;
    let number = that.data.number;
    let sqrt = number * number;
    arr.push(index);
    let left = index-1;
    let right = index+1;
    let lower = parseInt(index) + parseInt(number);
    let upper = index-number;
    if(Math.floor(left / number) == Math.floor(index / number)){
      arr.push(left);
    }
    if(Math.floor(index / number) ==  Math.floor(right / number)){
      arr.push(right);
    }
    if(lower < sqrt){
      arr.push(lower);
    }
    if(upper >= 0){
      arr.push(upper);
    }
    for(var i =0;i<arr.length;i++){
      var background1 = that.data.background[arr[i]];
      if(background1 == "#F7F7F7"){
        var boxColor = that.data.boxColor[that.data.boxColorIndex]
        that.setData({
          ['background['+arr[i]+']']: boxColor
        })
      }else{
        that.setData({
          ['background['+arr[i]+']']:"#F7F7F7"
        })
      }
    }
    that.data.count++;
    //判断是否胜利
    if(that.isWin()){
      wx.showModal({
        confirmText: '确定',
        showCancel: true,
        title: '恭喜顺利完成',
        content: that.data.count+"步完成!",
        success(result) {
          console.log(result)
          that.setData({
            count:0,
          })
          for(var i =0;i<that.data.background.length;i++){
              that.setData({
                ['background['+i+']']: "#F7F7F7"
              })
          }
        }
      })
    }
  },
  isWin:function(){
    for (let index = 0; index < this.data.background.length; index++) {
      const element = this.data.background[index];
      if(element ==  "#F7F7F7"){
        return false;
      }
    }
    return true;
  },
  clickNumber:function(e){
    app.addClickAudio(this.data.colorUrl);
    this.setData({
      count:0,
    })
    var number = e.currentTarget.dataset.number;
    this.loadTable(number);
  },
  loadTable:function(number){
    var that = this;
    var arrIndex = [];
    var arrColor = [];
    let pow = Math.pow(number,2)
    for (let index = 0; index < pow; index++) {
      arrIndex.push(index);
      arrColor.push("#F7F7F7");
    }
    // 修改盒子数量
    that.setData({
      number:number,
      background:arrColor,
      items:arrIndex
    })
  },
  // 修改颜色
  updateColor:function(e){
    var that = this;
    app.addClickAudio(this.data.colorUrl);
    var number = e.currentTarget.dataset.number;
    that.setData({
      boxColorIndex: number
    });
    for(var i =0;i<this.data.background.length;i++){
      var background1 = that.data.background[i];
      if(!(background1 == "#F7F7F7")){
        var boxColor = that.data.boxColor[that.data.boxColorIndex]
        that.setData({
          ['background['+i+']']: boxColor
        })
      }
    }
  },
  musicOpenOrClose:function(){
    //正在输出音频
    let isMusic = this.data.isMusic;
    url+'';
    if(isMusic){
      //关闭
      this.setData({
        isMusic: false,
      })
    }else{
      //开启
      this.setData({
        isMusic: true,
      })
    }
    console.log("1"+isMusic)
    app.addClickAudioLoopClose(isMusic)
  },
})
